import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:personinfo/utils/HistoryList.dart';
import 'package:personinfo/utils/commonUtil.dart';

import 'package:personinfo/widgets/common/CommonWidget.dart';



class CarResourceSearch extends StatefulWidget {
  const CarResourceSearch({super.key});

  @override
  State<StatefulWidget> createState() => _CarResourceSearchState();
}

class _CarResourceSearchState extends State<CarResourceSearch> {
  final TextEditingController _textController = TextEditingController();
  HistoryList history = HistoryList(len: 10);
  List listInfo = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    listInfo = history.get();
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      backgroundColor: Theme.of(context).colorScheme.onPrimary,
      appBar: commonWidget.appBar(
        title: const Text("车源搜索"),
        leading: GestureDetector(onTap: (){Navigator.pop(context);},child: const Icon(Icons.keyboard_arrow_left),),
      ),
      body: Stack(
        children: [
          Positioned(
            left: 0,
            top: 0,
            right: 0,
            child: Column(
              children: [
                Container(
                  padding: EdgeInsets.fromLTRB(16, 0, 16, 6),
                  child:  Row(
                    children: [
                      Flexible(
                          child: TextField(
                            controller: _textController,
                            decoration: const InputDecoration(
                              icon: Icon(Icons.search),
                              hintText: "搜索品牌/车系/车型/配置",
                              border: CommonUtil.underlineBlack,
                              focusedBorder:  CommonUtil.underlineBlack,
                          ),
                        )
                      ),
                      GestureDetector(
                        behavior: HitTestBehavior.translucent,
                        onTap: (){
                          List info = history.add(_textController.text);
                          setState(() {
                            listInfo = info;
                            Navigator.pushNamed(context, "/searchResult",arguments: {"keyWord": _textController.text});
                            _textController.text = "";
                          });

                        },
                        child: const Text("搜索",style: TextStyle(color: Color.fromRGBO(252, 105, 62, 1),fontSize: 15),),
                      )
                    ],
                  ),
                ),

                const Divider(height: 1,),
              ],
            )
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 58, 0, 0),
            child: ListView.builder(
              itemCount: listInfo.length,
                itemBuilder: (BuildContext context, index)=> GestureDetector(
                  onTap: (){
                    Navigator.pushNamed(context, "/searchResult",arguments: {"keyWord": listInfo[index]});
                  },
                  child: Column(
                    children: [
                      ListTile(title: Text(listInfo[index]),trailing: GestureDetector(
                        onTap: (){
                           setState(() {
                             listInfo = history.delete(listInfo[index]);
                           });
                        },
                        child: Icon(Icons.delete),
                      ),leading: Icon(Icons.timelapse),),
                      Divider(height: 1,),
                    ],
                  ),
                )
            ),
          ),

        ],
      ),
    );
  }
}
